//
//  ViewController.m
//  WangI
//
//  Created by wjn on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "ViewController.h"
#import "NewsHeaderModel.h"
#import "CustonCollectionViewCell.h"
#import "CustomFinanceCollectionViewCell.h"
#import "CustomRecreationCollectionViewCell.h"
#import "CustomSportCollectionViewCell.h"
#import "CustomHotCollectionViewCell.h"

#import "NetWorkTaskHandle.h"
#import "NewsFinanceModel.h"
#import "NewsRecreationModel.h"
#import "NewsSportModel.h"
#import "NewsHotModel.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) UILabel *labelHeader;
@property (nonatomic, retain) NewsHeaderModel *newsHeaderModel;
@property (nonatomic, retain) NewsFinanceModel *newsFinanceModel;
@property (nonatomic, retain) NewsRecreationModel *newsRecreatModel;
@property (nonatomic, retain) NewsSportModel *newsSportModel;
@property (nonatomic, retain) NewsHotModel *newsHotModel;

@end

@implementation ViewController


- (void)dealloc {
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"網易";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ImageRect"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image24h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    NSInteger n = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    UIButton *button = (UIButton *)[self.labelHeader viewWithTag:1000 + n];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    
    
    [self creatData];
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.labelHeader];
    
    [_labelHeader release];
    [_collectionView release];
    

    
}


#pragma mark -- UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
       CustonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCollectionViewCellIdentifier" forIndexPath:indexPath];

        cell.newHeaderModel = self.newsHeaderModel;
        
        return cell;
    } else if (indexPath.item == 1) {
        CustomRecreationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomRecreationCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.newsRecreationModel = self.newsRecreatModel;
        
        return cell;
    } else if (indexPath.item == 2) {
        CustomSportCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomSportCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.newsSportModel = self.newsSportModel;
        
        return cell;
    } else if (indexPath.item == 3) {
        CustomHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomHotCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.newsHotModel = self.newsHotModel;
        return cell;
    } else  {
        CustomFinanceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomFinanceCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.newsFinanceModel = self.newsFinanceModel;
        
        return cell;
    }

}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    NSInteger n = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    UIButton *button = (UIButton *)[self.labelHeader viewWithTag:1000 + n];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    for (int i = 0; i < 5; i++) {
        UIButton *button = (UIButton *)[self.labelHeader viewWithTag:1000 + i];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    
}

#pragma mark -- pravite
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height - 113);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 34, self.view.width, self.view.height ) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        
        [_collectionView registerClass:[CustonCollectionViewCell class] forCellWithReuseIdentifier:@"CustomCollectionViewCellIdentifier"];
        [_collectionView registerClass:[CustomFinanceCollectionViewCell class] forCellWithReuseIdentifier:@"CustomFinanceCollectionViewCellIdentifier"];
        [_collectionView registerClass:[CustomRecreationCollectionViewCell class] forCellWithReuseIdentifier:@"CustomRecreationCollectionViewCellIdentifier"];
        [_collectionView registerClass:[CustomSportCollectionViewCell class] forCellWithReuseIdentifier:@"CustomSportCollectionViewCellIdentifier"];
        [_collectionView registerClass:[CustomHotCollectionViewCell class] forCellWithReuseIdentifier:@"CustomHotCollectionViewCellIdentifier"];
    
    }
    return _collectionView;
}



-(UILabel *)labelHeader {
    if (!_labelHeader) {
        self.labelHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 30)];
        _labelHeader.userInteractionEnabled = YES;
        
        for (int i = 0; i < 5; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(i * self.view.width / 5, 0, self.view.width / 5, 30);
            button.tag = 1000 + i;
            if (i == 0) {
                [button setTitle:@"头条" forState:UIControlStateNormal];
            } else if (i == 1) {
                [button setTitle:@"娱乐" forState:UIControlStateNormal];
            } else if (i == 2) {
                [button setTitle:@"体育" forState:UIControlStateNormal];
            } else if (i == 3) {
                [button setTitle:@"科技" forState:UIControlStateNormal];
            } else if (i == 4) {
                [button setTitle:@"财经" forState:UIControlStateNormal];
            }
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [_labelHeader addSubview:button];
        }
    }
    return _labelHeader;
}



- (NSMutableArray *)dataSource {
    if(!_dataSource) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

#pragma mark -- creatData
- (void)creatData {
    
    UIActivityIndicatorView *cator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    cator.center = self.view.center;
    
    [self.collectionView addSubview:cator];
    [cator release];
    
    [cator startAnimating];
    
    [NetWorkTaskHandle sendTask:kNewsGETURL withResult:^(id result) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.newsHeaderModel = [[NewsHeaderModel alloc] initWithDataSource:result];
            [self.collectionView reloadData];
            [cator stopAnimating];
        });
       
    }];
    
    [NetWorkTaskHandle sendTask:kFinancegeturl withResult:^(id result) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.newsFinanceModel = [[NewsFinanceModel alloc] initWithDataSource:result];
            [self.collectionView reloadData];
        });
    }];
    
    [NetWorkTaskHandle sendTask:kRecreationGETURL withResult:^(id result) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.newsRecreatModel = [[NewsRecreationModel alloc] initWithDataSource:result];
            [self.collectionView reloadData];
        });
    }];
    
    [NetWorkTaskHandle sendTask:kSporetGETURL withResult:^(id result) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.newsSportModel = [[NewsSportModel alloc] initWithDataSource:result];
            [self.collectionView reloadData];
        });
    }];
    
    [NetWorkTaskHandle sendTask:kHotGETURL withResult:^(id result) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.newsHotModel = [[NewsHotModel alloc] initWithDataSource:result];
            [self.collectionView reloadData];
        });
        
    }];
}

#pragma  mark -- buttinAction

- (void)buttonAction:(UIButton *)button {
    
    for (int i = 0; i < 5; i++) {
        UIButton *button = (UIButton *)[self.labelHeader viewWithTag:1000 + i];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    
    [self.collectionView setContentOffset:CGPointMake(self.view.frame.size.width * (button.tag - 1000), -64) animated:YES];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    
}

- (void)rightBarButtonAction:(UIButton *)sender {
    
}

- (void)leftBarButtonAction:(UIButton *)sender {
    
}





#pragma mark -- 内存泄露
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
